﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using DataTransfer;
using Global;
using Microsoft.Win32;
using EnglishLibrary;

namespace EnglishReminder.Options
{
    public partial class Filters : Form
    {
        #region Attributes
        //Delegate function
        public delegate void RestartReminderForm();
        //delegate object
        public RestartReminderForm restartReminderForm;
        //the object to access and modify the registry
        private RegistryKey regMySoft;
        //before and after checkboxes
        private bool checkboxFlag;
        //The Datatransfer object
        private CategoryDT cateDT = new CategoryDT();
        #endregion

        #region Constructors and Load methods
        public Filters()
        {
            InitializeComponent();
        }
        private void Filters_Load(object sender, EventArgs e)
        {
            //temporary disable the checkbox, remove these two lines to open it
            this.chbBefore.Enabled = false;
            this.chbAfter.Enabled = false;

            LoadCheckboxes();
            LoadDateTimePicker();
            LoadCateListBoxFromSettings();

            //create the object the handle with the registry
            regMySoft = Utility.CreateRegistryKey();
        }
        #endregion

        #region Event handlers
        private void btnOk_Click(object sender, EventArgs e)
        {
            //Apply the items to show filters
            ApplyTheItemsToShowFilter();

            //Apply the Date filters
            ApplyTheDateFilter();

            //Apply the cate list boxes
            ApplyTheCateListBoxes();

            //restart the Reminder form
            if (restartReminderForm != null)
                restartReminderForm();

            this.Close();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void chbBefore_CheckedChanged(object sender, EventArgs e)
        {
            if (checkboxFlag == true)
            {
                chbAfter.Checked = false;
                checkboxFlag = false;
            }
        }
        private void chbAfter_CheckedChanged(object sender, EventArgs e)
        {
            if (checkboxFlag == false)
            {
                chbBefore.Checked = false;
                checkboxFlag = true;
            }
        }
        private void btnAddAll_Click(object sender, EventArgs e)
        {
            if (listbCateNoDisplay.Items.Count <= 0)
                return;

            foreach (var cate in listbCateNoDisplay.Items)
                listbCateDisplay.Items.Add(cate);
            
            listbCateNoDisplay.Items.Clear();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (listbCateNoDisplay.SelectedItems.Count <= 0)
                return;

            foreach (var cate in listbCateNoDisplay.SelectedItems)
            {
                listbCateDisplay.Items.Add(cate);
            }

            while (listbCateNoDisplay.SelectedItems.Count > 0)
            {
                listbCateNoDisplay.Items.Remove(listbCateNoDisplay.SelectedItems[0]);
            }
        }
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listbCateDisplay.SelectedItems.Count <= 0)
                return;

            foreach (var cate in listbCateDisplay.SelectedItems)
            {
                listbCateNoDisplay.Items.Add(cate);
            }

            while (listbCateDisplay.SelectedItems.Count > 0)
            {
                listbCateDisplay.Items.Remove(listbCateDisplay.SelectedItems[0]);
            }
        }
        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            if (listbCateDisplay.Items.Count <= 0)
                return;

            foreach (var cate in listbCateDisplay.Items)
                listbCateNoDisplay.Items.Add(cate);

            listbCateDisplay.Items.Clear();
        }
        private void btnSort_Click(object sender, EventArgs e)
        {
            //sort the listboxes when click button
            listbCateNoDisplay.Sorted = true;
            listbCateDisplay.Sorted = true;

            //after sorting, return it into normal state
            listbCateNoDisplay.Sorted = false;
            listbCateDisplay.Sorted = false;
        }
        #endregion

        #region Private methods
        private void ApplyTheItemsToShowFilter()
        {
            //Check valid
            if (chbVocabulary.Checked == false
                && chbNote.Checked == false)
            {
                Default.ReverseResultToDefault();
            }
            else
            {
                //save the values to the settings
                Settings.IS_VOCABULARY = chbVocabulary.Checked;
                Settings.IS_NOTE = chbNote.Checked;
            }

            //save the same values to the registry
            if (regMySoft != null)
            {
                using (var tempKey = regMySoft.OpenSubKey(Constant.REGISTRY.SETTINGS, true))
                {
                    if (tempKey != null)
                    {
                        tempKey.SetValue(Constant.REGISTRY.VOCABULARY, (Settings.IS_VOCABULARY == true) ? "1" : "0");
                        tempKey.SetValue(Constant.REGISTRY.NOTE, (Settings.IS_NOTE == true) ? "1" : "0");
                        tempKey.Close();
                    }
                }
            }
        }
        private void ApplyTheDateFilter()
        {
            //set the settings
            Settings.BEFORE_DATE = dtpBefore.Value.ToShortDateString();
            Settings.AFTER_DATE = dtpAfter.Value.ToShortDateString();

            //check checkboxes
            if (chbBefore.Checked)
            {
                Settings.IS_BEFORE_DATE_CHECKED = true;
            }

            if (chbAfter.Checked)
            {
                Settings.IS_AFTER_DATE_CHECKED = true;
            }

            //save the same values to the registry
            if (regMySoft != null)
            {
                using (RegistryKey tempKey = regMySoft.OpenSubKey(Constant.REGISTRY.SETTINGS, true))
                {
                    if (tempKey != null)
                    {
                        tempKey.SetValue(Constant.REGISTRY.BEFORE_DATE, Settings.BEFORE_DATE);
                        tempKey.SetValue(Constant.REGISTRY.AFTER_DATE, Settings.AFTER_DATE);
                        tempKey.SetValue(Constant.REGISTRY.IS_BEFORE_DATE_CHECKED, (Settings.IS_BEFORE_DATE_CHECKED == true) ? "1" : "0");
                        tempKey.SetValue(Constant.REGISTRY.IS_AFTER_DATE_CHECKED, (Settings.IS_AFTER_DATE_CHECKED == true) ? "1" : "0");
                        tempKey.Close();
                    }
                }
            }
        }
        private void ApplyTheCateListBoxes()
        {
            Settings.CATEGORY_LIST_DISPLAY.Clear();
            Settings.CATEGORY_LIST_NO_DISPLAY.Clear();

            foreach (KeyValuePair<int, string> cate in listbCateDisplay.Items)
                Settings.CATEGORY_LIST_DISPLAY.Add(cate.Key, cate.Value);

            foreach (KeyValuePair<int, string> cate in listbCateNoDisplay.Items)
                Settings.CATEGORY_LIST_NO_DISPLAY.Add(cate.Key, cate.Value);
        }
        private void LoadCheckboxes()
        {
            //Load checkboxes
            chbVocabulary.Checked = Settings.IS_VOCABULARY;
            chbNote.Checked = Settings.IS_NOTE;
            chbBefore.Checked = Settings.IS_BEFORE_DATE_CHECKED;
            chbAfter.Checked = Settings.IS_AFTER_DATE_CHECKED;
        }
        private void LoadCateListBoxFromSettings()
        {
            foreach (var cate in Settings.CATEGORY_LIST_NO_DISPLAY)
            {
                listbCateNoDisplay.Items.Add(cate);
            }

            foreach (var cate in Settings.CATEGORY_LIST_DISPLAY)
            {
                listbCateDisplay.Items.Add(cate);
            }
        }
        private void LoadDateTimePicker()
        {
            if (Settings.BEFORE_DATE != string.Empty)
                dtpBefore.Value = DateTime.Parse(Settings.BEFORE_DATE);
            else dtpBefore.Value = DateTime.Now;

            if (Settings.AFTER_DATE != string.Empty)
                dtpAfter.Value = DateTime.Parse(Settings.AFTER_DATE);
            else dtpAfter.Value = DateTime.Now;
        }
        #endregion
    }
}
